package com.trex.modules.tbl.lineform.service;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.trex.modules.tbl.entity.ServerBxConfig;
import com.trex.modules.tbl.replyform.dao.TblXdJxchsdDao;
import com.trex.modules.tbl.replyform.entity.TblXdJxchsd;
import com.trex.modules.tbl.service.TblXdLjzyjlService;

import com.trex.modules.tbl.workerplace.dao.TblZdWorkerdzDao;
import com.trex.modules.utils.JsonGenerator;
import org.apache.poi.hwpf.usermodel.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.sys.utils.UserUtils;
import com.trex.modules.tbl.lineform.entity.TblXdZxxdctzd;
import com.trex.modules.tbl.entity.TblXdLjzyjl;
import com.trex.modules.tbl.lineform.dao.TblXdZxxdctzdDao;

import static com.trex.modules.tbl.web.TblXdLjzyjlController.parseJsonToListOfMap;
import static com.trex.modules.utils.JsonGeneratorDetail.createJson;
import static com.trex.modules.utils.JsonSenderUtil.sendJsonWithHttpClient;
import static java.lang.Thread.sleep;


/**
 * 站修线倒车通知单Service
 * @author jy
 * @version 2023-02-04
 */
@Service
public class TblXdZxxdctzdService extends CrudService<TblXdZxxdctzdDao, TblXdZxxdctzd> {


	@Autowired
	private TblXdZxxdctzdDao tblXdZxxdctzdDao;
	@Autowired
	private TblXdLjzyjlService tblXdLjzyjlService;
	@Autowired
	private TblXdJxchsdDao tblXdJxchsdDao;
	@Autowired
	private TblZdWorkerdzDao tblZdWorkerdzDao;

	private final ServerBxConfig serverBxConfig;

    public TblXdZxxdctzdService(ServerBxConfig serverBxConfig) {
        this.serverBxConfig = serverBxConfig;
    }

    /**
	 * 获取单条数据
	 * @param tblXdZxxdctzd
	 * @return
	 */
	@Override
	public TblXdZxxdctzd get(TblXdZxxdctzd tblXdZxxdctzd) {
		return super.get(tblXdZxxdctzd);
	}
	
	/**
	 * 查询分页数据
	 * @param tblXdZxxdctzd 查询条件
	 * @param tblXdZxxdctzd.page 分页对象
	 * @return
	 */
	@Override
	public Page<TblXdZxxdctzd> findPage(TblXdZxxdctzd tblXdZxxdctzd) {
		Page<TblXdZxxdctzd> page = super.findPage(tblXdZxxdctzd);
		return page;
	}
	
	/**
	 * 查询列表数据
	 * @param tblXdZxxdctzd
	 * @return
	 */
	@Override
	public List<TblXdZxxdctzd> findList(TblXdZxxdctzd tblXdZxxdctzd) {
		if (tblXdZxxdctzd.getRemark()!=null){
			if (tblXdZxxdctzd.getRemark().equals("3")) {
				return tblXdZxxdctzdDao.findInfo(tblXdZxxdctzd.getRecId());
			}
			List<TblXdZxxdctzd> list1 = tblXdZxxdctzdDao.findList1(tblXdZxxdctzd);
			return list1;
		}else{
			return super.findList(tblXdZxxdctzd);

		}
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param tblXdZxxdctzd
	 */
	@Override
	@Transactional
	public void save(TblXdZxxdctzd tblXdZxxdctzd) {
		super.save(tblXdZxxdctzd);
	}
	
	/**
	 * 更新状态
	 * @param tblXdZxxdctzd
	 */
	@Override
	@Transactional
	public void updateStatus(TblXdZxxdctzd tblXdZxxdctzd) {
		super.updateStatus(tblXdZxxdctzd);
	}
	
	/**
	 * 删除数据
	 * @param jhhString
	 * @throws SQLException 
	 */
	@Transactional (rollbackFor=Exception.class)
	public  String delete(String jhhString) throws SQLException {
		String[] strArray = jhhString.split(",");
		tblXdZxxdctzdDao.upDateFlay(strArray[0]);
		return "success";
	}
//线路查询
    public List<TblXdZxxdctzd> selectLine() {
		List<TblXdZxxdctzd> stringList = tblXdZxxdctzdDao.selectLine();
		return stringList;
    }
//根据用户查线路
	public List<TblXdZxxdctzd> selectLine1(String userName) {
		List<TblXdZxxdctzd> stringList = tblXdZxxdctzdDao.selectLine1(userName);
		return stringList;
	}
//根据线路查站台
    public List<TblXdLjzyjl> getAllLineListByStation(String stationCode,String userCode){
		return tblXdZxxdctzdDao.getAllLineListByStation(stationCode,userCode);
	}


	/**
	 * 站修线确认
	 * 1列检调度确认 2 车站确认 3列检调度撤销
	 * @param map 
	*/
	@Transactional (rollbackFor=Exception.class)
	public String confirm(String ids, HttpServletRequest request) throws Exception {

		String I_IP_ADDR = request.getRemoteHost(); // 取IP
		String I_USERCODE = UserUtils.getUser().getUserCode(); // 取用户id
		String I_USERNAME = UserUtils.getUser().getUserName(); //用户名
//		HttpSession session = request.getSession();
//		String sessionId = session.getId();
//		String userId = tblXdLjzyjlService.selectUserData(sessionId);
		String userId = JsonGenerator.extractAndDecodeAccessToken(request);
		logger.info("9999999999999999999999+_____" + userId);

		String userName = tblZdWorkerdzDao.selectName1(userId);

		String jnrName = tblXdLjzyjlService.selectJnrName(userId);
		String[] idN = ids.split(",");
		
		for(String s : idN) {
			TblXdZxxdctzd tblXdZxxdctzd = get(s);
			if(tblXdZxxdctzd.getDest() == null || tblXdZxxdctzd.getDest().isEmpty())
			{
				throw new SQLException(tblXdZxxdctzd.getWagonno() + "没有填写去向，不能确认");
			}
			if("1".equals(tblXdZxxdctzd.getStatus()))
			{
				throw new SQLException(tblXdZxxdctzd.getWagonno() + "已经列检调度确认");
			}
			if("2".equals(tblXdZxxdctzd.getStatus()))
			{
				throw new SQLException(tblXdZxxdctzd.getWagonno() + "已经车站调度确认");
			}
			HashMap<String, Object> map = new HashMap<>();
			map.put("I_TZDBH", s); 	// rec_id
			map.put("I_FLAG", "1");	// /*1列检调度确认 2 车站确认 3 列检调度撤销*/
			map.put("I_USERCODE", userId); //登录用户
			map.put("I_USERNAME", userName); //登录用户
			map.put("I_IP", I_IP_ADDR); // 本机IP地址
			
			tblXdZxxdctzdDao.confirmData(map);

			String flag = map.get("O_FLAG").toString();
			if (flag.equals("0")) {
					tblXdZxxdctzd = tblXdZxxdctzdDao.selectData(s);
					//成功
					long timestamp = System.currentTimeMillis();
					// 将时间戳转换为 Date 对象
					Date date = new Date(timestamp);

					// 使用 SimpleDateFormat 格式化时间戳为 yyyyMMddHHmmss 格式的字符串
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
					String formattedDate = sdf.format(date);
					String timestampWithLast4Millis = timestamp + String.format("%04d", timestamp % 10000);
				ObjectMapper objectMapper = new ObjectMapper();

					/*第一次*/
					Map<String, Object> sysInfo1 = Map.of(
							"Flag", 0,
							"Msg", "",
							"SvcName", "NEM218",
							"UUID", timestampWithLast4Millis
					);

					// BODY 表的 Columns 数据
					List<Map<String, String>> bodyColumns1 = List.of(
							Map.of("Name", "OPERATE_FLAG", "DataType", "S", "Caption", "操作标志"),
							Map.of("Name", "REC_ID", "DataType", "S", "Caption", "记录ID"),
							Map.of("Name", "POUR_OUT_TIME", "DataType", "S", "Caption", "倒出时间"),
							Map.of("Name", "IS_OUT_STAGE", "DataType", "S", "Caption", "是否出场"),
							Map.of("Name", "DEST", "DataType", "S", "Caption", "去向"),
							Map.of("Name", "OPERATOR", "DataType", "S", "Caption", "操作人"),
							Map.of("Name", "CONFM_NUMBER", "DataType", "S", "Caption", "确认人"),
							Map.of("Name", "PROCESSING_TIME", "DataType", "S", "Caption", "办理时间"),
							Map.of("Name", "CCZK", "DataType", "S", "Caption", "出场状况"),
							Map.of("Name", "ZXZZ", "DataType", "S", "Caption", "专项整治"),
							Map.of("Name", "SFJF", "DataType", "S", "Caption", "是否禁峰"),
							Map.of("Name", "WAGONNO", "DataType", "S", "Caption", "车号"),
							Map.of("Name", "ZXDD", "DataType", "S", "Caption", "转修地点"),
							Map.of("Name", "VEHICLEREPAIRSTATUS", "DataType", "S", "Caption", "修程")
					);

					// BODY 表的 Rows 数据
					List<List<Object>> bodyRows1 = new ArrayList<>();
					List<Object> row1 = new ArrayList<>();

// 向 row 添加元素
					row1.add("I");
					row1.add(tblXdZxxdctzd.getJxcbh() != null ? tblXdZxxdctzd.getJxcbh() : "");
					row1.add(formattedDate != null ? formattedDate : "");
					row1.add(tblXdZxxdctzd.getIsOutStage() != null ? tblXdZxxdctzd.getIsOutStage() : "");
					row1.add(tblXdZxxdctzd.getDest() != null ? tblXdZxxdctzd.getDest() : "");
					row1.add(userName != null ? userName : "");
					row1.add(userName != null ? userName : "");
					row1.add(formattedDate != null ? formattedDate : "");
					row1.add(tblXdZxxdctzd.getCczk() != null ? tblXdZxxdctzd.getCczk() : "");
					row1.add(tblXdZxxdctzd.getZxzz() != null ? tblXdZxxdctzd.getZxzz() : "");
					row1.add(tblXdZxxdctzd.getIsNopeak() != null ? tblXdZxxdctzd.getIsNopeak() : "");
					row1.add(tblXdZxxdctzd.getWagonno() != null ? tblXdZxxdctzd.getWagonno() : "");
					row1.add(tblXdZxxdctzd.getZXDD() != null ? tblXdZxxdctzd.getZXDD() : "");
					row1.add(tblXdZxxdctzd.getVehicleRepairStatus() != null ? tblXdZxxdctzd.getVehicleRepairStatus() : "");



// 将 row 添加到 bodyRows 中
					bodyRows1.add(row1);
// 转换为 key-value 结构的 map
					List<Map<String, Object>> keyValueMessage1 = new ArrayList<>();
					for (List<Object> rowData1 : bodyRows1) {
						Map<String, Object> rowMap1 = new HashMap<>();
						for (int j = 0; j < bodyColumns1.size(); j++) {
							String key = bodyColumns1.get(j).get("Name");
							Object value = rowData1.get(j);
							rowMap1.put(key, value);
						}
						keyValueMessage1.add(rowMap1);
					}


					// 构建最终的 Tables 结构
					Map<String, Object> tables1 = Map.of(
							"BODY", keyValueMessage1
					);

					// 构建最终的电文结构
					Map<String, Object> finalMessage1 = Map.of(
							"SysInfo", sysInfo1,
							"Tables", tables1
					);

					// 使用 Jackson 将 Map 转换为 JSON 字符串
					String json1=objectMapper.writeValueAsString(finalMessage1);



//					String json1 = createJson(sysInfo1, keyValueMessage1);
					logger.info("@@@@@@@@@@@@@@@" + json1);

				String s11 = sendJsonWithHttpClient(serverBxConfig.getAddress(), serverBxConfig.getPort(), serverBxConfig.getMethod(), json1);

					sleep(1000);

				// SysInfo 数据
				Map<String, Object> sysInfo = Map.of(
						"Flag", 0,
						"Msg", "",
						"SvcName", "NEM219",
						"UUID", timestampWithLast4Millis
				);

				// BODY 表的 Columns 数据
				List<Map<String, String>> bodyColumns = List.of(
						Map.of("Name", "OPERATE_FLAG", "DataType", "S", "Caption", "操作标志"),
						Map.of("Name", "REC_ID", "DataType", "S", "Caption", "记录ID"),
						Map.of("Name", "WORK_TIME", "DataType", "S", "Caption", "确认时间"),
						Map.of("Name", "WORK_NUMBER", "DataType", "S", "Caption", "确认人"),
						Map.of("Name", "STATUS", "DataType", "S", "Caption", "状态"),
						Map.of("Name", "WAGONNO", "DataType", "S", "Caption", "车号")
				);

				// BODY 表的 Rows 数据
				List<List<Object>> bodyRows = new ArrayList<>();
				List<Object> row = new ArrayList<>();

// 向 row 添加元素
				row.add("I");
				row.add(tblXdZxxdctzd.getJxcbh() != null ? tblXdZxxdctzd.getJxcbh() : "");
				row.add(formattedDate);
				row.add(userName);
				row.add("1");
				row.add(tblXdZxxdctzd.getWagonno() != null ? tblXdZxxdctzd.getWagonno() : "");

// 将 row 添加到 bodyRows 中
				bodyRows.add(row);


				// 转换为 key-value 结构的 map
				List<Map<String, Object>> keyValueMessage = new ArrayList<>();
				for (List<Object> rowData : bodyRows) {
					Map<String, Object> rowMap = new HashMap<>();
					for (int i1 = 0; i1 < bodyColumns.size(); i1++) {
						String key = bodyColumns.get(i1).get("Name");
						Object value = rowData.get(i1);
						rowMap.put(key, value);
					}
					keyValueMessage.add(rowMap);
				}
// 构建最终的 Tables 结构
				Map<String, Object> tables = Map.of(
						"BODY", keyValueMessage
				);

				// 构建最终的电文结构
				Map<String, Object> finalMessage = Map.of(
						"SysInfo", sysInfo,
						"Tables", tables
				);

				// 使用 Jackson 将 Map 转换为 JSON 字符串

				String json=objectMapper.writeValueAsString(finalMessage);

				logger.info("@@@@@@@@@@@@@@@" + json);
				//测试环境
//					String s1 = sendJsonWithHttpClient("172.16.16.36", 8084, "/bx", json);
				String s1 = sendJsonWithHttpClient(serverBxConfig.getAddress(), serverBxConfig.getPort(), serverBxConfig.getMethod(), json);
				logger.info("@@@@@@@@@@@@@@@" + s1);

				}else{
				throw new SQLException(text(map.get("O_MESS").toString()));
			}
		}
return "success";
	}

	/**
	 * 站修线取消
	 * 1列检调度确认 2 车站确认 3列检调度撤销
	 * @param map 
	*/
	@Transactional(rollbackFor=Exception.class)
	public String cancel(String ids, HttpServletRequest request) throws Exception {
		String I_IP_ADDR = request.getRemoteHost(); // 取IP
//		HttpSession session = request.getSession();
//		String sessionId = session.getId();
//		String userId = tblXdLjzyjlService.selectUserData(sessionId);
		String userId = JsonGenerator.extractAndDecodeAccessToken(request);
		logger.info("9999999999999999999999+_____" + userId);
		String userName = tblZdWorkerdzDao.selectName1(userId);
		String[] idN = ids.split(",");
		for(String s : idN) {
			TblXdZxxdctzd tblXdZxxdctzd = get(s);
			if("0".equals(tblXdZxxdctzd.getStatus()))
			{
				throw new SQLException(tblXdZxxdctzd.getWagonno() + "未确认");
			}
			if("2".equals(tblXdZxxdctzd.getStatus()))
			{
				throw new SQLException(tblXdZxxdctzd.getWagonno() + "车站调度已确认");
			}
			HashMap<String, Object> map = new HashMap<>();
			map.put("I_TZDBH", s); 	// rec_id
			map.put("I_FLAG", "3");	// /* 1列检调度确认 2 车站确认 3 列检调度撤销*/
			map.put("I_USERCODE", userId); //登录用户
			map.put("I_USERNAME", userName); //登录用户
			map.put("I_IP", I_IP_ADDR); // 本机IP地址
			//确认取消调用同一个过程
			tblXdZxxdctzdDao.confirmData(map);
			
			String flag = map.get("O_FLAG").toString();
			if (flag.equals("0")) {
				long timestamp = System.currentTimeMillis();
				// 将时间戳转换为 Date 对象
				Date date = new Date(timestamp);

				// 使用 SimpleDateFormat 格式化时间戳为 yyyyMMddHHmmss 格式的字符串
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				String formattedDate = sdf.format(date);
				String timestampWithLast4Millis = timestamp + String.format("%04d", timestamp % 10000);
				// SysInfo 数据
				Map<String, Object> sysInfo = Map.of(
						"Flag", 0,
						"Msg", "",
						"SvcName", "NEM219",
						"UUID", timestampWithLast4Millis
				);

				// BODY 表的 Columns 数据
				List<Map<String, String>> bodyColumns = List.of(
						Map.of("Name", "OPERATE_FLAG", "DataType", "S", "Caption", "操作标志"),
						Map.of("Name", "REC_ID", "DataType", "S", "Caption", "记录ID"),
						Map.of("Name", "WORK_TIME", "DataType", "S", "Caption", "确认时间"),
						Map.of("Name", "WORK_NUMBER", "DataType", "S", "Caption", "确认人"),
						Map.of("Name", "STATUS", "DataType", "S", "Caption", "状态"),
						Map.of("Name", "WAGONNO", "DataType", "S", "Caption", "车号")
				);

				// BODY 表的 Rows 数据
				List<List<Object>> bodyRows = List.of(
						List.of("D", tblXdZxxdctzd.getJxcbh() != null ? tblXdZxxdctzd.getJxcbh() : "",formattedDate, userName,"1",tblXdZxxdctzd.getWagonno())
				);


				// 转换为 key-value 结构的 map
				List<Map<String, Object>> keyValueMessage = new ArrayList<>();
				for (List<Object> rowData : bodyRows) {
					Map<String, Object> rowMap = new HashMap<>();
					for (int i1 = 0; i1 < bodyColumns.size(); i1++) {
						String key = bodyColumns.get(i1).get("Name");
						Object value = rowData.get(i1);
						rowMap.put(key, value);
					}
					keyValueMessage.add(rowMap);
				}
// 构建最终的 Tables 结构
				Map<String, Object> tables = Map.of(
						"BODY", keyValueMessage
				);

				// 构建最终的电文结构
				Map<String, Object> finalMessage = Map.of(
						"SysInfo", sysInfo,
						"Tables", tables
				);

				// 使用 Jackson 将 Map 转换为 JSON 字符串
				ObjectMapper objectMapper = new ObjectMapper();
				String json=objectMapper.writeValueAsString(finalMessage);

				logger.info("@@@@@@@@@@@@@@@"+json);
				//测试环境
//				String s1 = sendJsonWithHttpClient("172.16.16.36", 8084, "/bx", json);
				String s1 = sendJsonWithHttpClient(serverBxConfig.getAddress(), serverBxConfig.getPort(), serverBxConfig.getMethod(), json);
//				logger.info("@@@@@@@@@@@@@@@"+s1);

				/*第二次*/
				Map<String, Object> sysInfo1 = Map.of(
						"Flag", 0,
						"Msg", "",
						"SvcName", "NEM218",
						"UUID", timestampWithLast4Millis
				);

				// BODY 表的 Columns 数据
				List<Map<String, String>> bodyColumns1 = List.of(
						Map.of("Name", "OPERATE_FLAG", "DataType", "S", "Caption", "操作标志"),
						Map.of("Name", "REC_ID", "DataType", "S", "Caption", "记录ID"),
						Map.of("Name", "POUR_OUT_TIME", "DataType", "S", "Caption", "倒出时间"),
						Map.of("Name", "IS_OUT_STAGE", "DataType", "S", "Caption", "是否出场"),
						Map.of("Name", "DEST", "DataType", "S", "Caption", "去向"),
						Map.of("Name", "OPERATOR", "DataType", "S", "Caption", "操作人"),
						Map.of("Name", "CONFM_NUMBER", "DataType", "S", "Caption", "确认人"),
						Map.of("Name", "PROCESSING_TIME", "DataType", "S", "Caption", "办理时间"),
						Map.of("Name", "CCZK", "DataType", "S", "Caption", "出场状况"),
						Map.of("Name", "ZXZZ", "DataType", "S", "Caption", "专项整治"),
						Map.of("Name", "SFJF", "DataType", "S", "Caption", "是否禁峰"),
						Map.of("Name", "WAGONNO", "DataType", "S", "Caption", "车号"),
						Map.of("Name", "ZXDD", "DataType", "S", "Caption", "转修地点"),
						Map.of("Name", "VEHICLEREPAIRSTATUS", "DataType", "S", "Caption", "修程")

				);

				// BODY 表的 Rows 数据
				List<List<Object>> bodyRows1 = new ArrayList<>();
				List<Object> row1 = new ArrayList<>();

// 向 row 添加元素
				row1.add("D");
				row1.add(tblXdZxxdctzd.getJxcbh() != null ? tblXdZxxdctzd.getJxcbh() : "");
				row1.add(formattedDate != null ? formattedDate : "");
				row1.add(tblXdZxxdctzd.getIsOutStage() != null ? tblXdZxxdctzd.getIsOutStage() : "");
				row1.add(tblXdZxxdctzd.getDestName() != null ? tblXdZxxdctzd.getDestName() : "");
				row1.add(userName != null ? userName : "");
				row1.add(userName != null ? userName : "");
				row1.add(formattedDate != null ? formattedDate : "");
				row1.add(tblXdZxxdctzd.getCczk() != null ? tblXdZxxdctzd.getCczk() : "");
				row1.add(tblXdZxxdctzd.getZxzz() != null ? tblXdZxxdctzd.getZxzz() : "");
				row1.add(tblXdZxxdctzd.getIsNopeak() != null ? tblXdZxxdctzd.getIsNopeak() : "");
				row1.add(tblXdZxxdctzd.getWagonno() != null ? tblXdZxxdctzd.getWagonno() : "");
				row1.add(tblXdZxxdctzd.getZXDD() != null ? tblXdZxxdctzd.getZXDD() : "");
				row1.add(tblXdZxxdctzd.getVehicleRepairStatus() != null ? tblXdZxxdctzd.getVehicleRepairStatus() : "");



// 将 row 添加到 bodyRows 中
				bodyRows1.add(row1);
// 转换为 key-value 结构的 map
				List<Map<String, Object>> keyValueMessage1 = new ArrayList<>();
				for (List<Object> rowData1 : bodyRows1) {
					Map<String, Object> rowMap1 = new HashMap<>();
					for (int j = 0; j < bodyColumns1.size(); j++) {
						String key = bodyColumns1.get(j).get("Name");
						Object value = rowData1.get(j);
						rowMap1.put(key, value);
					}
					keyValueMessage1.add(rowMap1);
				}


				// 构建最终的 Tables 结构
				Map<String, Object> tables1 = Map.of(
						"BODY", keyValueMessage1
				);

				// 构建最终的电文结构
				Map<String, Object> finalMessage1 = Map.of(
						"SysInfo", sysInfo1,
						"Tables", tables1
				);

				// 使用 Jackson 将 Map 转换为 JSON 字符串
				String json1=objectMapper.writeValueAsString(finalMessage1);



//					String json1 = createJson(sysInfo1, keyValueMessage1);
				logger.info("@@@@@@@@@@@@@@@" + json1);
				//测试环境
//				String s11 = sendJsonWithHttpClient("172.16.16.36", 8084, "/bx", json1);

				String s11 = sendJsonWithHttpClient(serverBxConfig.getAddress(), serverBxConfig.getPort(), serverBxConfig.getMethod(), json1);


			}else {
				throw new SQLException(text(map.get("O_MESS").toString()));
			}
		}
return "success";
	}

	
    public List<TblXdZxxdctzd> selectLinereply(String userid) {
		List<TblXdZxxdctzd> stringList = tblXdZxxdctzdDao.selectLinereply(userid);
		return stringList;
    }

	public List<TblXdLjzyjl> getSomeLineListByStation() {
		List<TblXdLjzyjl> stringList = tblXdZxxdctzdDao.getSomeLineListByStation();
		return stringList;
	}


	public void receiveCar(JSONObject json) throws JsonProcessingException, ParseException {
		// 创建 ObjectMapper 对象
		ObjectMapper objectMapper = new ObjectMapper();


		JsonNode rootNode = objectMapper.readTree(json.toString());

		// 解析 BODY 并放入 Map 中
		JsonNode bodyNode = rootNode.path("Tables").path("BODY").get(0); // 假设 BODY 只有一个对象
		Map<String, String> bodyMap = new HashMap<>();
		bodyMap.put("OPERATION_FLAG", bodyNode.path("OPERATION_FLAG").asText());
		bodyMap.put("FLAG", bodyNode.path("FLAG").asText());
		bodyMap.put("STATUS", bodyNode.path("STATUS").asText());
		bodyMap.put("STATION_USER_CODE", bodyNode.path("STATION_USER_CODE").asText());
		bodyMap.put("STATION_USER_NAME", bodyNode.path("STATION_USER_NAME").asText());
		bodyMap.put("STATION_TIME", bodyNode.path("STATION_TIME").asText());
if (bodyMap.put("OPERATION_FLAG", bodyNode.path("OPERATION_FLAG").asText()).equals("I")) {
	JsonNode detailArray = rootNode.path("Tables").path("DETAIL");
	List<Map<String, String>> detailList = new ArrayList<>();
	for (JsonNode detailNode : detailArray) {
		Map<String, String> detailMap = new HashMap<>();
		detailMap.put("REC_ID", detailNode.path("REC_ID").asText());
		detailMap.put("WAGONNO", detailNode.path("WAGONNO").asText());
		detailList.add(detailMap);
	}
	String FLAG = bodyMap.get("FLAG").toString();

	String stationTimeStr = bodyMap.get("STATION_TIME"); // 示例日期 20240906084500
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); // 使用正确的格式
	Date stationTime = dateFormat.parse(stationTimeStr); // 转换为 Date


	if ("1".equals(FLAG)) {
		for (Map<String, String> stringStringMap : detailList) {
			tblXdJxchsdDao.updateInfo(stringStringMap.get("REC_ID"), bodyMap.get("STATUS"), bodyMap.get("STATION_USER_CODE"), bodyMap.get("STATION_USER_NAME"), stationTime);
		}


	} else if ("2".equals(FLAG)) {

		for (Map<String, String> stringStringMap : detailList) {
			tblXdZxxdctzdDao.updateInfo(stringStringMap.get("REC_ID"), bodyMap.get("STATUS"), bodyMap.get("STATION_USER_CODE"), bodyMap.get("STATION_USER_NAME"), stationTime);
		}
	}
}
	}
//查去向
	public String selectdest(String dest) {
		return tblXdZxxdctzdDao.selectDest(dest);
	}
//查车号
	public String selectWagonno(String string) {
		return tblXdZxxdctzdDao.selectWagonno(string);
	}

	public String selectDeviceid(String id) {
		return tblXdZxxdctzdDao.selectDeviceid(id);
	}

	public String selectTzdbh(String id) {
		return tblXdZxxdctzdDao.selectTzdbh(id);
	}

	public String selectSeq(String id) {
		return tblXdZxxdctzdDao.selectSeq(id);
	}
}